<template>
  <component
    wd="900"
    :is="cfg.model"
    v-model:window_open="val.window_open"
    title="我的招募信息"
  >
    <template #content>
      <div class="model" ref="model">
        <template v-if="cfg.user?.authkey == null">
          <div class="stitle">· 登录后查看招募信息 ·</div>
          <n-button :size="cfg.sizebtn" @click="cfg.loginif = true"
            >登录</n-button
          ></template
        >
        <template v-else>
          <div class="_box0">
            <div class="_box1">
              <n-spin :show="val.loading">
                <n-grid x-gap="8" y-gap="8" :cols="parseInt(val.width / 275)">
                  <n-gi span="1" v-for="item in val.data">
                    <div class="tbox nbox3">
                      <div>
                        <div
                          style="text-align: center; color: #dec46a; margin-bottom: 8px"
                        >
                          {{ item.guild_name }}
                        </div>
                        <div class="center">
                          <div class="mb_font_sm" style="width: 13em">
                            招募到期：<n-time
                              :time="item.time"
                              format="MM-dd hh:mm"
                              unix
                            />
                          </div>
                          <div class="mb_font_sm" style="width: 13em">
                            招募设置：{{ item.settime ?? 0 }} 天
                          </div>
                        </div>
                        <n-hr />
                        <n-space justify="center">
                          <n-button
                            size="small"
                            type="gt_w"
                            @click="ct.refresh_time(item._id)"
                            >刷新时间</n-button
                          >
                          <n-button
                            size="small"
                            type="gt_y"
                            @click="ct.upd_info(item._id)"
                            >更改信息</n-button
                          >
                          <n-button
                            size="small"
                            type="gt_n"
                            @click="ct.del_info(item._id)"
                            >删除招募</n-button
                          >
                        </n-space>
                      </div>
                    </div>
                  </n-gi>
                </n-grid>
              </n-spin>
              <div></div>
            </div>
          </div>
        </template>
      </div>
    </template>
    <template #footer> </template>
  </component>
</template>
<script>
import { cfg } from "@/cfg/cfg.js";
export default defineComponent({
  props: {
    window_open: Boolean,
  },
  setup(props, { emit }) {
    const model = ref(null);
    let val = reactive({
      width: 0,
      window_open: false,
      sub: {},
      data: [],
      loading: false,
    });
    let ct = reactive({
      //刷新时间
      refresh_time: async (id) => {
        try {
          val.loading = true;
          id = id.$oid;
          let res = await cfg.axios.get(`${cfg.url}`, {
            params: {
              t: "v2/get",
              t2: "introduction/recruit",
              t3: "refresh",
              authkey: cfg.user?.authkey,
              uid: cfg.user?.uid,
              id,
            },
          });
          if (res.data?.code == 0) {
            emit("event-refresh", id);
            ct.load_guild();
            return;
          } else {
            val.loading = false;
            cfg.msg.error(res.data?.msg);
            return;
          }
        } catch (error) {
          val.loading = false;
          cfg.msg.error(error);
          return;
        }
      },
      //删除招募公会
      del_info: async (id) => {
        try {
          val.loading = true;
          id = id.$oid;
          let res = await cfg.axios.get(`${cfg.url}`, {
            params: {
              t: "v2/get",
              t2: "introduction/recruit",
              t3: "del_guild",
              authkey: cfg.user?.authkey,
              uid: cfg.user?.uid,
              id,
            },
          });
          if (res.data?.code == 0) {
            emit("event-refresh", id);
            ct.load_guild();
            return;
          } else {
            val.loading = false;
            cfg.msg.error(res.data?.msg);
            return;
          }
        } catch (error) {
          val.loading = false;
          cfg.msg.error(error);
          return;
        }
      },
      //更新信息
      upd_info: (id) => {
        try {
          id = id.$oid;
          emit("event-updinfo", id, "gupd");
        } catch (error) {
          cfg.msg.error(error);
        }
      },
      //加载公会
      load_guild: async (id) => {
        try {
          val.loading = true;
          let res = await cfg.axios.get(`${cfg.url}`, {
            params: {
              t: "v2/get",
              t2: "introduction/recruit",
              t3: "load_my_guild",
              authkey: cfg.user?.authkey,
              uid: cfg.user?.uid,
            },
          });
          if (res.data?.code == 0) {
            val.data = res.data.data;
            val.loading = false;
            return;
          } else {
            val.loading = false;
            cfg.msg.error(res.data?.msg);
            return;
          }
          return;
        } catch (error) {
          val.loading = false;
          cfg.msg.error(error);
        }
      },
    });
    watch(
      () => val.window_open,
      (newVal) => {
        if (!newVal) {
          setTimeout(() => {
            emit("update:window_open", false);
          }, 250);
        }
      }
    );

    //监听窗口变化事件
    const handleResize = () => {
      if (val.window_open == true) {
        const width = model.value?.offsetWidth;
        val.width = width;
      }
    };
    //热更新时移除监听
    watchEffect(() => {
      window.removeEventListener("resize", handleResize);
    });
    //监听窗口变化
    onMounted(() => {
      val.window_open = true;
      ct.load_guild();
      nextTick(() => {
        window.addEventListener("resize", handleResize);
        handleResize();
      });
    });
    //关闭时候移除监听
    onUnmounted(() => {
      window.removeEventListener("resize", handleResize);
    });

    return {
      cfg,
      props,
      ct,
      val,
      model,
    };
  },
  //载入完成
});
</script>
